package tn.bergaoui.openwebapp.wicket.pages;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import tn.bergaoui.openwebapp.dao.QuestionDao;
import tn.bergaoui.openwebapp.model.Question;
import tn.bergaoui.openwebapp.wicket.components.ISelectionListener;

/**
 * Shows a list of people.
 * 
 * @author Bergaoui Bacem
 */
public class QuestionListPanel extends ListPanel<Question, QuestionDao> {
	
	public QuestionListPanel(String id, ISelectionListener listener,
			Component component) {
		super(id, listener, component);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean(name="questionDao")
	private QuestionDao questionDao;
	

	@Override
	protected Question createDefaultEntity() {
		Question question = new Question();
		question.setContent("donner une question");
		
		return question;
	}

	@Override
	protected ListView createListView(final Component component) {
		IModel hateList = new LoadableDetachableModel() {
			protected Object load() {
				return loadEntitis();
			}
		};
		
		ListView listView = new ListView("listview", hateList) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			protected void populateItem(ListItem item) {
				final Question question = (Question) item.getModelObject();
				AjaxLink link = new AjaxLink( "idQHyper") {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						QuestionListPanel.this.listener.onSelectedItem(question.getId(), question.getClass());
						
						if (target !=null)  {
							target.addComponent(component);
						}
												
					}
				};
				
				
				link.add(new Label("idQ", question.getId().toString()));
				item.add(link);
				//item.add(new Label("firstName", player.getFirstName()));
				item.add(new Label("content", question.getContent()));

			}

		};
		
		return listView;
	}

	@Override
	public QuestionDao getGenericDao() {
		return questionDao;
	}

	public void setQuestionDao(QuestionDao questionDao) {
		this.questionDao = questionDao;
	}

	public QuestionDao getQuestionDao() {
		return questionDao;
	}
	

}
